package com.itjiange;

import java.util.List;

/**
 * 抽象处理类
 *
 * @author ItJianGe
 * @data 2024/8/19 10:34
 */
public abstract class AbstractProcess {

    private AbstractProcess nextAbstractProcess;

    /**
     * 处理邮件
     *
     * @param emails
     */
    public final void handleMessage(List<Email> emails) {
        List<Email> emailList = emails.stream()
                .filter(email -> email.getEmailType() == this.getEmailType()).toList();
        this.execute(emailList);
        // 如果下一个责任链不为空，则下一个责任链进行处理
        if (this.nextAbstractProcess == null) return;
        this.nextAbstractProcess.handleMessage(emails);
    }

    /**
     * 设置下一个责任链
     *
     * @param nextAbstractProcess
     */
    public void setNextAbstractProcess(AbstractProcess nextAbstractProcess) {
        this.nextAbstractProcess = nextAbstractProcess;
    }

    /**
     * 获取邮件类型
     *
     * @return
     */
    public abstract int getEmailType();

    /**
     * 处理邮件
     *
     * @param emails
     */
    public abstract void execute(List<Email> emails);
}
